package com.fingard.dsp.bank.directbank.whccb01;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;

/**
 * Created by dalt on 2017/7/10.
 */
public class WHCCB01Balance extends WHCCB01Base implements IBalance {

    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        String opname = "enterpriseQry";
        try {
            while (p_req7506.readRecord()) {//不支持批量查询，逐个查询
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();

                //写入通用的头部节点
                Map<String, String> params = new HashMap<String, String>();
                params.put("accountNo", p_req7506.readedDetail.actNum);
                params.put("currencyType", convertCurToBank(p_req7506.readedDetail.actCur));

                String[] tmpStrRet = sendToBank(params, p_req7506.reqHeader, opname);
                //如果返回tmpStrRet[0]表示返回connMsg，connMsg 为空则表示响应正常，
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        //获取银行响应代码，返回值为000或002表示处理成功，否则为处理失败
                        String tmpRetCode = getRespCode(tmpBkReader);
                        if (success(String.valueOf(tmpRetCode))) {
                            if (tmpBkReader.read("/BankData/opRep/RepParam")) {
                                tmpRetDetail.respCode = "0";
                                //获取币种
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                //获取账户
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                                //获取可用余额，如果balSign为“+”表示金额为正，为“-”表示金额为负
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balanceAvailable");
                                //获取冻结金额
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("balanceFreeze");
                                //获取实际余额
                                //Double currBal = Double.valueOf(tmpRetDetail.availBal) + Double.valueOf(tmpRetDetail.frozenBal);
                                tmpRetDetail.currBal = new BigDecimal(tmpBkReader.readedXElement.getSingleText("balance")).toPlainString();
                            }
                        }
                        //处理失败respCode返回为-1
                        else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = getErrorMsg(tmpBkReader);
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
